package com.gdut.simple.presenter.user;

import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.contract.HistoryContract;
import com.gdut.simple.data.entity.History;
import com.gdut.simple.model.IHistoryModel;
import com.gdut.simple.model.impl.HistoryModelImpl;

import java.util.List;

public class HistoryPresenter implements HistoryContract.Presenter {

    private HistoryContract.View mHistoryView;
    private IHistoryModel mHistoryModel;

    public HistoryPresenter(HistoryContract.View view) {
        this.mHistoryView = view;
        this.mHistoryModel = new HistoryModelImpl();
        this.mHistoryView.setPresenter(this);
    }

    @Override
    public void load() {
        mHistoryView.showRefreshProgress();
        mHistoryModel.queryHistory(new RequestCallback<List<History>>() {

            @Override
            public void onSuccess(List<History> histories) {
                mHistoryView.hideRefreshProgress();
                mHistoryView.getItemList().clear();
                mHistoryView.getItemList().addAll(histories);
                mHistoryView.refreshList();
            }

            @Override
            public void onFailure(String message) {
                mHistoryView.hideRefreshProgress();
                mHistoryView.showMessage(message);
            }
        });
    }

    @Override
    public void delete(History history) {
        mHistoryModel.deleteHistory(history);
        mHistoryView.refreshList();
    }

    @Override
    public void clear() {
        mHistoryModel.clear();
        mHistoryView.getItemList().clear();
        mHistoryView.refreshList();
    }
}
